package com.yuandian.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yuandian.constant.Constants;
import com.yuandian.enums.S;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * 说明：日期工具类
 * @Author zfsn
 * @Date 2022/10/25 9:47
 */
public final class YDateUtil extends DateUtil {

    private static final ZoneOffset ZONE_OFFSET = ZoneOffset.of("+8");

	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static String YYYYMMDD = "yyyyMMdd";

	public static String HH_MM = "HH:mm";

	public static String  YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

	public static String HH_MM_SS= "HH:mm:ss";

	public static String YYYY_MM_DD = "yyyy-MM-dd";

	public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH";

	private static final DateTimeFormatter yyyyMMddFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");

	private static final DateTimeFormatter HH_MM_SSFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

    private static final DateTimeFormatter yyyy_MM_dd_HH_mm_ssFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter yyyy_MM_dd_HH_mm_ss_SSSSSSFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");

	private static final String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};


	public static String mm_dd(String strDate) {
		try {
			String dateStr = strDate.substring(0, 10);
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
			return new SimpleDateFormat("MM-dd").format(dateVal);
		} catch (Exception ex) {
 			return null;
		}
	}


	/**
	 * 把日期转换为（yyyy-MM-dd）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}


	/**
	 * 计算两个时间差
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return day + "天" + hour + "小时" + min + "分钟";
	}


	/**
	 * 根据开始时间、结束时间、返回值格式、时间间隔 自动生成X轴数据
	 *
	 * @param starTime	开始时间
	 * @param endTime	结束时间
	 * @param formatterStr 返回值格式
	 * @param timeInterval 时间间隔
	 * @return
	 */
	public static List<String> getTimeListByJDK8(String starTime, String endTime, String formatterStr,  int timeInterval) {
		DateTimeFormatter returnFormatter = DateTimeFormatter.ofPattern(formatterStr);
		LocalDateTime startDateTime = LocalDateTime.parse(starTime, yyyy_MM_dd_HH_mm_ssFormatter);
		LocalDateTime endDateTime = LocalDateTime.parse(endTime, yyyy_MM_dd_HH_mm_ssFormatter);
		List<String> timeList = new ArrayList<>();

		//遍历从startTime 到 endTime 时间间隔的所有时间点
		while (startDateTime.isBefore(endDateTime)) {
			timeList.add(startDateTime.format(returnFormatter));
			startDateTime = startDateTime.plusMinutes(timeInterval);
		}
		return timeList;
	}

	/**
	 * 计算 currentDate 距离第二天 零点 的总秒数
	 * @param currentDate	时间
	 * @return 秒数
	 */
	public static Integer getRemainSecondsOneDay(Date currentDate) {
		//使用plusDays加传入的时间加1天，将时分秒设置成0
		LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),
						ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
				.withSecond(0).withNano(0);
		LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
				ZoneId.systemDefault());
		//使用ChronoUnit.SECONDS.between方法，传入两个LocalDateTime对象即可得到相差的秒数
		long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
		return (int) seconds;
	}



	/**
	 * 计算时间间隔, 以分钟为单位返回
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static long timeDistance(String startTime, String endTime){
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		// 把日期时间格式化
		LocalDateTime start = LocalDateTime.parse(startTime, formatter);
		LocalDateTime end = LocalDateTime.parse(endTime, formatter);
		Duration minutesDuration = Duration.between(start, end);
		long minutes = minutesDuration.toMinutes();

		return minutes;
	}


	public static final Date dateTime(final String format, final String ts) {
		try {
			return new SimpleDateFormat(format).parse(ts);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 根据时间得到该时间提前多少分钟的时间 例如：如minute是15，即得到的时间是：该时间减去15分钟
	 *
	 * @param nowTime 时间
	 * @param minute 提前分钟数
	 * @return 结果
	 */
	@SuppressWarnings("static-access")
	public static Date getBeforeMinute(Date nowTime, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.MINUTE, -minute);
		return c.getTime();
	}


	/**
	 * 把字符串转换为日期（格式：yyyy-MM-dd HH:mm:ss）
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date stringFormatDate(String dateStr){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String getTimeStr(LocalDateTime date) {
		return date.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
	}

	/**
	 * 把（yyyy-MM-dd HH:mm）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public static Date yyyy_MM_dd_HH_mm(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return sdf.parse(strDate);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 把（yyyy-MM-dd HH:mm）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public static String yyyy_MM_dd_000000(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return yyyy_MM_dd_HH_mm_ss(sdf.parse(strDate));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据开始时间、结束时间、时间间隔 自动生成X轴数据
	 * @param starTime	开始时间
	 * @param endTime	结束时间
	 * @param timeInterval 时间间隔
	 * @return 结果集
	 */
	public static List<String> getTimeList(String starTime, String endTime, int timeInterval) {

		List<String> timeList = new ArrayList<>();
		int year = 0, month = 0, day = 0, hour = 0, min = 0;
		Date startDate = yyyy_MM_dd_HH_mm(starTime);
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		year = startCalendar.get(Calendar.YEAR);
		month = startCalendar.get(Calendar.MONTH);
		day = startCalendar.get(Calendar.DAY_OF_MONTH);
		hour = startCalendar.get(Calendar.HOUR_OF_DAY);
		// 向前归属
		min = startCalendar.get(Calendar.MINUTE) / timeInterval * timeInterval;
		startCalendar.set(year, month, day, hour, min);
		startDate = startCalendar.getTime();
		starTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startDate);

		Date endDate = yyyy_MM_dd_HH_mm(endTime);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		year = endCalendar.get(Calendar.YEAR);
		month = endCalendar.get(Calendar.MONTH);
		day = endCalendar.get(Calendar.DAY_OF_MONTH);
		hour = endCalendar.get(Calendar.HOUR_OF_DAY);
		// 向前归属
		min = endCalendar.get(Calendar.MINUTE) / timeInterval * timeInterval;
		endCalendar.set(year, month, day, hour, min);
		endDate = endCalendar.getTime();
		endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endDate);

		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		// 把日期时间格式化
		LocalDateTime start = LocalDateTime.parse(starTime, formatter);
		LocalDateTime end = LocalDateTime.parse(endTime, formatter);

		while(!start.equals(end)) {
			timeList.add(start.toString().replace("T", " ") + ":00.0");
			start = start.plusMinutes(timeInterval);
		}
		timeList.add(end.toString().replace("T", " ") + ":00.0");

		return timeList;
	}

	/**
	 * 比较日期大小
	 * @param date 	参数一
	 * @param date1	参数二
	 * @return 结果集
	 */
	public static Long compare(String date, String date1) {
		Date date2 = parse(date);
		Date date3 = parse(date1);
		return getMillis(date2) - getMillis(date3);
	}

	public static long getMillis(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	/**
	 * 基于 nowDateTime 往后延迟 minute 分钟
	 * @param nowDateTime 时间
	 * @param minute 延迟的分钟数
	 * @return 返回延迟后的时间 yyyy-MM-dd HH:mm:ss
	 */
	public static String getAfterMinuteStr(String nowDateTime, int minute){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(nowDateTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.MINUTE, minute);
		c.set(c.SECOND, 00);
		return sdf.format(c.getTime());
	}

    /**
     * 格式化LocalDateTime为 yyyyMMdd 字符串格式的时间
     * @param localDateTime localDateTime
     * @return yyyyMMdd字符串
     */
    public static String yyyyMMdd(LocalDateTime localDateTime) {
        return yyyyMMddFormatter.format(localDateTime);
    }

    /**
     * 将 yyyy-MM-dd HH:mm:ss 字符串格式的字符串格式化为 yyyyMMdd 字符串格式的时间
     * @param dateStr yyyy_MM_dd_HH_mm_ss字符串
     * @return yyyyMMdd字符串
     */
    public static String yyyyMMdd(String dateStr) {
        return dateStr.replaceAll(S.CROSS_BAR.s(), "").substring(0, 8);
    }

    /**
     * 格式化LocalDateTime为 yyyy-MM-dd HH:mm:ss 字符串格式的时间
     * @param localDateTime localDateTime
     * @return yyyy_MM_dd_HH_mm_ss字符串
     */
    public static String yyyy_MM_dd_HH_mm_ss(LocalDateTime localDateTime) {
        return yyyy_MM_dd_HH_mm_ssFormatter.format(localDateTime);
    }

    /**
     * 将 yyyy-MM-dd HH:mm:ss 字符串格式的时间转为时间戳(秒)
     * @param dateStr yyyy_MM_dd_HH_mm_ss字符串
     * @return 时间戳
     */
    public static long yyyy_MM_dd_HH_mm_ss(String dateStr) {
        return LocalDateTime.parse(dateStr, yyyy_MM_dd_HH_mm_ssFormatter).toEpochSecond(ZONE_OFFSET);
    }

	/**
	 * 把日期转换为（yyyy-MM-dd HH:mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ss(Date date) {
		if (date != null) {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf1.format(date);
		}
		return null;
	}

    /**
     * 将单位为秒的时间戳转换为 yyyy-MM-dd HH:mm:ss 字符串格式的时间
     * @param second 单位为秒的时间戳
     * @return 格式化字符串
     */
    public static String yyyy_MM_dd_HH_mm_ssBySecond(long second) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZONE_OFFSET);
        return yyyy_MM_dd_HH_mm_ss(localDateTime);
    }

    /**
     * 将单位为毫秒的时间戳转换为 yyyy-MM-dd HH:mm:ss 字符串格式的时间
     * @param millisecond 单位为毫秒的时间戳
     * @return 格式化字符串
     */
    public static String yyyy_MM_dd_HH_mm_ssByMillisecond(long millisecond) {
        return yyyy_MM_dd_HH_mm_ssBySecond(millisecond / 1_000);
    }

    /**
     * 将单位为微秒的时间戳转换为 yyyy-MM-dd HH:mm:ss 字符串格式的时间
     * 100w次调用耗时：326ms
     * @param microsecond 单位为微秒的时间戳
     * @return 格式化字符串
     */
    public static String yyyy_MM_dd_HH_mm_ssByMicrosecond(long microsecond) {
        return yyyy_MM_dd_HH_mm_ssBySecond(microsecond / 1_000_000);
    }

    /**
     * 将单位为微秒的时间戳转换为 yyyy-MM-dd HH:mm:ss.SSSSSS 字符串格式的时间
     * @param microsecond 单位为微秒的时间戳
     * @return 格式化字符串
     */
    public static String yyyy_MM_dd_HH_mm_ss_SSSSSS(long microsecond) {
        long epochSecond = microsecond / 1_000_000;
        int nanoOfSecond = (int) ((microsecond % 1_000_000) * 1000);
        return yyyy_MM_dd_HH_mm_ss_SSSSSSFormatter.format(
                LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, ZONE_OFFSET));
    }

    /**
     * 获取当前时间戳
     * @return 当前时间戳(秒)
     */
    public static long currentTimeStamp() {
        return LocalDateTime.now().toEpochSecond(ZONE_OFFSET);
    }

    /**
     * 将单位为秒的时间戳转换为 yyyy-MM-dd HH:mm:00 字符串格式的时间(整分)
     * @param second 单位为秒的时间戳
     * @return 格式化字符串
     */
    public static String getWholeMinute(long second) {
        return yyyy_MM_dd_HH_mm_ss(getWholeMinute(
                LocalDateTime.ofEpochSecond(second, 0, ZONE_OFFSET), 0));
    }

    /**
     * 获取localDateTime的n个整分
     * @param localDateTime localDateTime
     * @param n n
     * @return 结果
     */
    public static LocalDateTime getWholeMinute(LocalDateTime localDateTime, int n) {
        LocalDateTime wholeLocalDateTime = localDateTime.plusMinutes(n);
        return wholeLocalDateTime
                .toLocalDate()
                .atTime(wholeLocalDateTime.getHour(), wholeLocalDateTime.getMinute(), 0);
    }

    /**
     * 获取localDateTime的n个整时
     * @param localDateTime localDateTime
     * @param n n
     * @return 结果
     */
    public static LocalDateTime getWholeHour(LocalDateTime localDateTime, int n) {
        LocalDateTime wholeLocalDateTime = localDateTime.plusHours(n);
        return wholeLocalDateTime
                .toLocalDate()
                .atTime(wholeLocalDateTime.getHour(), 0, 0);
    }

    /**
     * 获取localDateTime的n个整天
     * @param localDateTime localDateTime
     * @param n n
     * @return 结果
     */
    public static LocalDateTime getWholeDay(LocalDateTime localDateTime, int n) {
        LocalDateTime wholeLocalDateTime = localDateTime.plusDays(n);
        return wholeLocalDateTime
                .toLocalDate()
                .atTime(0, 0, 0);
    }

	/**
	 * 时间格式化
	 * @param format
	 * @param date
	 * @return
	 */
	public static final String parseDateToStr(final String format, final Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 往后延后多少天
	 * @param timeStr	时间参数
	 * @param date	要延后的天数
	 * @return 结果
	 */
	public static String getAfterDay(String timeStr, int date) {
		Date nowTime = stringFormatDate(timeStr);
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.DATE, +date);
		return yyyy_MM_dd_HH_mm_ss(c.getTime());
	}


	/**
	 * 往后延后多少小时
	 * @param timeStr	时间参数
	 * @param hour	要延后的小时
	 * @return 结果
	 */
	public static String getAfterHour(String timeStr, int hour) {
		Date nowTime = stringFormatDate(timeStr);
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.HOUR, +hour);
		return yyyy_MM_dd_HH_mm_ss(c.getTime());
	}

    /**
     * 往后推一天, 时间设为 00:00:00
     * @param timeStr 指定时间参数
     * @return
     */
    public static String getAfterDay(String timeStr) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            Date dd = df.parse(timeStr);
            calendar.setTime(dd);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }catch (Exception e) {
            e.printStackTrace();
        }
        String st = df.format(calendar.getTime());
        st = st+" 00:00:00";

        return st;
    }

	/**
	 * 时间切割
	 *
	 * @param type     切割类型
	 * @param starDate 开始时间
	 * @param endDate  结束时间
	 * @return 切割后的时间结果集
	 */
	public static List<String> splitDate(String type, Date starDate, Date endDate, boolean isContainStr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		List<String> listDate = new ArrayList<>();
		Calendar starCalendar = Calendar.getInstance();
		starCalendar.setTime(starDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		while (endCalendar.after(starCalendar)) {
			switch (type) {
				case "M":
					starCalendar.add(Calendar.MONTH, 1);
					break;
				case "D":
					starCalendar.add(Calendar.DAY_OF_YEAR, 1);
					break;
				case "H":
					starCalendar.add(Calendar.HOUR, 1);
					break;
				case "N":
					starCalendar.add(Calendar.SECOND, 1);
					break;
			}
			if (endCalendar.after(starCalendar)) {
				listDate.add(sdf.format(starCalendar.getTime()));
			} else {
				if (isContainStr) {
					listDate.add(0, sdf.format(starDate.getTime()));
				} else {
					listDate.add(sdf.format(endDate.getTime()));
				}
			}
		}
		return listDate;
	}

	/**
	 * 时间切割
	 *
	 * @param type     切割类型
	 * @param starDate 开始时间
	 * @param endDate  结束时间
	 * @return 切割后的时间结果集
	 */
	public static List<String> splitDate(String type,int var, Date starDate, Date endDate, boolean isContainStr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		List<String> listDate = new ArrayList<>();
		Calendar starCalendar = Calendar.getInstance();
		starCalendar.setTime(starDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		while (endCalendar.after(starCalendar)) {
			switch (type) {
				case "M":
					starCalendar.add(Calendar.MONTH, var);
					break;
				case "D":
					starCalendar.add(Calendar.DAY_OF_YEAR, var);
					break;
				case "H":
					starCalendar.add(Calendar.HOUR, var);
					break;
				case "mm":
					starCalendar.add(Calendar.MINUTE, var);
					break;
				case "N":
					starCalendar.add(Calendar.SECOND, var);
					break;
			}
			if (endCalendar.after(starCalendar)) {
				listDate.add(sdf.format(starCalendar.getTime()));
			} else {
				if (isContainStr) {
					listDate.add(0, sdf.format(starDate.getTime()));
				} else {
					listDate.add(sdf.format(endDate.getTime()));
				}
			}
		}
		return listDate;
	}

	/**
	 * 获取时间段
	 *
	 * @param date  时间
	 * @param staHour 开始小时
	 * @param endHour 结束小时
	 * @return 时间段
	 */
	public static Map<String,String> getTimePiece(Date date, Integer staHour, Integer endHour) {
		Map<String,String> map = new HashMap<>(2);
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, staHour);
		calendar.set(Calendar.MINUTE, 00);
		calendar.set(Calendar.SECOND, 00);
		map.put("startTime", sdf.format(calendar.getTime()));
		calendar.set(Calendar.HOUR_OF_DAY, endHour);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		map.put("endTime", sdf.format(calendar.getTime()));
		return map;
	}


	/**
	 *  获取时间段
	 * @param date 当前时间
	 * @param staHour 开始小时
	 * @param endHour 结束小时
	 * @param startMin 开始分钟
	 * @param endMin 结束分钟
	 * @param startSec 开始秒
	 * @param endSec 结束秒
	 * @return 时间段
	 */
	public static Map<String,String> getTimePiece(Date date, Integer staHour, Integer endHour,Integer startMin, Integer endMin,Integer startSec, Integer endSec) {
		Map<String,String> map = new HashMap<>(2);
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, staHour);
		calendar.set(Calendar.MINUTE, startMin);
		calendar.set(Calendar.SECOND, startSec);
		map.put("startTime", sdf.format(calendar.getTime()));
		calendar.set(Calendar.HOUR_OF_DAY, endHour);
		calendar.set(Calendar.MINUTE, endMin);
		calendar.set(Calendar.SECOND, endSec);
		map.put("endTime", sdf.format(calendar.getTime()));
		return map;
	}

	/**
	 * 字符串型日期转时间戳
	 */
	public static Long getUsecFromDateStr(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		try {
			return sdf.parse(dateStr).getTime() * 1000;
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 根据传入的时间获取该时间的分钟
	 *
	 * @param date 时间
	 * @return 分钟0~59
	 */
	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

	/**
	 * 根据传入的时间获取该时间的小时
	 *
	 * @param date 时间
	 * @return 小时0~23
	 */
	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 根据传入的时间获取该时间的月
	 *
	 * @param date 时间
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH);
	}

	/**
	 * 根据传入的时间获取该时间的年
	 *
	 * @param date 时间
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 根据日期获取星期几
	 *
	 * @param date 日期
	 * @return 星期几
	 */
	public static String dateToWeek(String date) {
		Date newDate = dateTime(YYYY_MM_DD, date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(newDate);
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekDays[w];
	}

	/**
	 * 获取过去几天日期
	 *
	 * @param date   时间
	 * @param past   天数
	 * @param format 格式化
	 * @return
	 */
	public static String getPastDate(String date, int past, String format) {
		Date newDate = dateTime(YYYY_MM_DD, date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(newDate);
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		return parseDateToStr(format, calendar.getTime());
	}

	/**
	 * 获取过去几天日期
	 *
	 * @param date   时间
	 * @param past   天数
	 * @param format 格式化
	 * @return
	 */
	public static String getPastDate2(String date, int past, String format) {
		Date newDate = dateTime(YYYY_MM_DD_HH_MM_SS, date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(newDate);
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		return parseDateToStr(format, calendar.getTime());
	}

	/**
	 * 传进来的时间(ms毫秒)
	 *
	 * 保留两位小数，如:2 会在后面补上00 即2.00
	 * 1）   如果时长/耗时大于等于60000ms 则格式化为 “XX分XX.XX秒”保留两位小数
	 * 2）   如果时长/耗时小于60000ms大于等于1000ms则格式化为“XX.XX秒”保留两位小数
	 * 3）   如果时长/耗时小于1000ms则格式化为“XX.XX毫秒”保留两位小数
	 * 如:
	 * 204266ms  显示为  3分24.27秒
	 * 2008ms    显示为  2.01秒（4舍5入）
	 * 4ms       显示为  4.00毫秒（4舍5入）
	 *
	 * @param data 传进来的时间(ms毫秒)
	 * @returns
	 */
	public static String getMinSecond(double data) {
		StringBuilder sb = new StringBuilder();

		if(data >= 60000){
			long minute = (long)Math.floor(data / 60000);
			sb.append(minute);
			sb.append("分");

			double second = data % 60000 / 1000;
			if(second != 0) {
				String secondStr = String.format("%.2f",second);
				sb.append(secondStr);
				sb.append("秒");
			}
		}else if(data >= 1000){
			long second =(long) Math.floor(data / 1000);
			double millisecond = data % 1000 / 1000;

			sb.append(String.format("%.2f", (second + millisecond))).append("秒");
		}else{
			sb.append(String.format("%.2f", data)).append("毫秒");
		}
		return sb.toString();
	}

	/**
	 * 把日期转换为（yyyy-MM-dd 00:00:00"）格式的字符串
	 *
	 * @param date 时间
	 * @return 结果集
	 */
	public static String yyyy_MM_dd_00_00_00(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		return sdf.format(yyyy_MM_dd_HH_mm_ss(date));
	}


	/**
	 * 校验是否跨天
	 *
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static boolean checkTime(String startTime, String endTime){
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constants.DATETIME_FORMAT);
		LocalDateTime startTimeL = LocalDateTime.parse(startTime, formatter);
		LocalDateTime endTimeL = LocalDateTime.parse(endTime, formatter);
		// 计算查询时间间隔
		Duration minutesDuration = Duration.between(startTimeL, endTimeL);
		long minutes = minutesDuration.toMinutes();
		// 获取开始时间+1天后的日期
		LocalDate startDate = startTimeL.plusDays(1).toLocalDate();
		// 校验是否跨天
		return (minutes > 1440) || LocalDateTime.of(startDate, LocalTime.MIN).isBefore(endTimeL);
	}

	/**
	 * 把"2021-11-21 12:00:00" 转换成 "2021年11月21日"
	 * @param strDate
	 * @return
	 */
	public static String yyyy_mm_dd(String strDate) {
		try {
			String dateStr = strDate.substring(0, 10);
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
			String resData = new SimpleDateFormat("yyyy年MM月dd日").format(dateVal);
			return resData;
		} catch (Exception ex) {
			return null;
		}
	}


	/**
	 * 把毫秒转换为（yyyy-MM-dd HH:mm:ssss）格式的字符,精确到毫秒
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ssSSS(Long timeUsec) {
		Date date = new Date(timeUsec);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return sdf.format(date);
	}


	/**
	 * 获取前几天的同一时间值
	 * @param time 时间
	 * @param days 天数
	 * @return
	 */
	public static String getBeforeDayTime(String time,Integer days) {
		boolean timeExits = ObjectUtil.isNotEmpty(time);
		Calendar calendar = Calendar.getInstance();
		if(timeExits){
			DateTime parse = parse(time);
			calendar.setTime(parse);
		}else {
			calendar.setTime(new Date());
			calendar.set(Calendar.HOUR_OF_DAY,0);
			calendar.set(Calendar.MINUTE,0);
			calendar.set(Calendar.SECOND,0);
		}
		calendar.add(Calendar.DATE, -days);
		Date beforeTime = calendar.getTime();
		return yyyy_MM_dd_HH_mm_ss(beforeTime);
	}

	/**
	 * 切割时间区间
	 * 		如把 20230-07-08 09:00:00 -  20230-07-10 09:00:00
	 * 		切割成 ：
	 * 			 (20230-07-08 09:00:00 -  20230-07-09 00:00:00)
	 * 			 (20230-07-09 00:00:00 -  20230-07-10 00:00:00)
	 * 			 (20230-07-10 00:00:00 -  20230-07-10 09:00:00)
	 */
	public static List<Map<String, String>> timeCutting(String startTime, String endTime) {
		List<Map<String, String>> list = new ArrayList<>();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

		LocalDateTime startDateTime = LocalDateTime.parse(startTime, formatter);
		LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter);

		LocalDateTime currentDateTime = startDateTime;
		while (currentDateTime.isBefore(endDateTime)) {
			Map<String, String> map = new LinkedHashMap<>();
			LocalDateTime nextDateTime = currentDateTime.plusDays(1).withHour(0).withMinute(0).withSecond(0);
			if (nextDateTime.isAfter(endDateTime)) {
				nextDateTime = endDateTime;
			}

			map.put("startTime", currentDateTime.format(formatter));
			map.put("endTime", nextDateTime.format(formatter));
			list.add(map);
			System.out.println(currentDateTime.format(formatter) + " - " + nextDateTime.format(formatter));

			currentDateTime = nextDateTime;
		}
		return list;
	}


	public static String yyyyMMdd(Date srcDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(srcDate);
	}

	/**
	 * 把日期转换为（HH:mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String HH_mm_ss(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String mm_ss(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（HH）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String HH(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
		return sdf.format(date);
	}

	/**
	 * 把（HH:mm:ss）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public static Date HH_mm_ss(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			return sdf.parse(strDate);
		} catch (Exception ex) {
			return null;
		}
	}

	public static String yyyy(Date srcDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return sdf.format(srcDate);
	}

	public static String MM(Date srcDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		return sdf.format(srcDate);
	}

	/**
	 * 把日期转换为（yyyyMMddHHmmss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMMddHHmmss(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyyMM）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMM(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyyMMddHHmmss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMMddHHmmssSSS(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyyMMddHH）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMMddHH(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
		return sdf.format(date);
	}

	/**
	 * 把日期字符串(yyyy-MM-dd HH:mm:ss)转换为（yyyyMMddHHmmss）格式的字符串
	 *
	 * @param strDateTime
	 * @return
	 */
	public static String yyyyMMddHHmmss(String strDateTime) {
		if (strDateTime == null || strDateTime.equals("")) {
			return null;
		}
		String str = strDateTime.replaceAll("-", "");
		str = str.replaceAll(" ", "");
		str = str.replaceAll(":", "");
		return str;
	}

	/**
	 * 把日期转换为(yyyyMMddHHmm)格式的字符串
	 *
	 * @param datetime
	 * @return
	 */
	public static String yyyyMMddHHmm(Date datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		return sdf.format(datetime);
	}

	/**
	 * 把传入时间转化为时间字符串(HHmmss格式)
	 *
	 * @param datetime
	 * @return
	 */
	public static String HHmmss(Date datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		return sdf.format(datetime);
	}
}
